//
//  ZKStatusViewModel.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/28.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

//封装微博cell对应的视图
class ZKStatusViewModel: NSObject {
    
    //引用模型
    var status:StatusModel? {
        
        didSet{
            
            //处理会员等级显示的图片
            disposeMembershipImg(member: status?.user?.mbrank ?? 0 )
            //处理认证类型显示的图片
            disposeAvatarImgType(avatarImgType: status?.user?.verified_type ?? -1)
            //处理转发微博内容
            if let currentStatus = status{
                disposeRetweetContent(status: currentStatus)
            }
            //处理来源数据
            disposeSourceContent(source: status?.source ?? "")
        }
    }
    
    
    //会员等级图片
    var membershipImg: UIImage?
    
    //认证类型图片
    var avatarImg:UIImage?
    
    //转发微博内容
    var retweetContent: String?
    
    //来源数据
    var sourceContent: String?
    
    //时间数据
    var timeContent:String?{
        
        guard let createAt = status?.created_at else{
            
            return nil
        }
        let dt = DateFormatter()
        //获取时间对象
        if isThisYear(createdAt: createAt) {
            //今年
            //创建日历对象
            let calendar = Calendar.current
            //判断是否今天
            if calendar.isDateInToday(createAt) {
                //今天
                //获取时间差对应的秒数,距离现在多少时间
                let timeInterval = abs(createAt.timeIntervalSinceNow)
                //判断是否是1分钟之前
                if timeInterval < 60 {
                    return "刚刚"
                }else if timeInterval < 3600 {
                    //是否是一小时之前
                    let resutlt = timeInterval / 60
                    return "\(Int(resutlt))分钟前"
                }else{
                    //其他(多少小时前)
                    let resutlt = timeInterval / 3600
                    return "\(Int(resutlt))小时前"
                }
                
            }else if calendar.isDateInYesterday(createAt){
                //昨天
                dt.dateFormat = "昨天 HH:mm"
            }else{
                //其他(月份+时间)
                dt.dateFormat = "MM-dd HH:mm"
            }
        }else{
            //不是今年
            dt.dateFormat = "yyyy-MM-dd HH:mm"
        }
        
        return dt.string(from: createAt)
    }
    
    //判断微博发布时间是否是今年
    private func isThisYear(createdAt: Date)->Bool{
        let dt = DateFormatter()
        //年份格式化
        dt.dateFormat = "yyyy"
        //获取发微博时间的年份
        let creatAtyear = dt.string(from: createdAt)
        
        //获取今年时间年份
        let currentyear = dt.string(from: Date())
        
        return creatAtyear == currentyear
        
    }
    //处理来源数据
    private func disposeSourceContent(source: String) {
        
        //获取开始截取的范围,获取结束截取的范围
        if let startRange = source.range(of: "\">"),let endRange = source.range(of: "</"){
            //获取开始截取的光标索引upperBound:表示在指定范围内的最大光标位置
            let startIndex = startRange.upperBound
            //获取结束截取的光标索引lowerBound:表示在指定范围内的最小光标位置
            let endIndex = endRange.lowerBound
            //根据范围截取指定的字符串
            let subStr = source.substring(with: startIndex..<endIndex)
            
            //拼接来源数据
            sourceContent = "来自" + subStr
        }
    }
    
    //处理转发微博内容
    private func disposeRetweetContent(status:StatusModel) {
        
        if let retweetStatus = status.retweeted_status {
            
            //表示有转发内容,获取用户昵称
            if let screen_name = retweetStatus.user?.screen_name,let text = retweetStatus.text{
                
                //昵称和转发微博一定有值,进行字符串的拼接
                retweetContent = "@\(screen_name): \(text)"
                
            }
            
        }
        
        
    }
    
    //处理认证类型图片(avatarImgType:数子对应的图片)
    private func disposeAvatarImgType(avatarImgType:Int){
        //认证等级: -1:没有认证,0:认证用户,2 3 5:企业用户,220:微博达人
        switch avatarImgType {
        case 0:
            avatarImg = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            avatarImg = UIImage(named: "avatar_enterprise_vip")
        case 220:
            avatarImg = UIImage(named: "avatar_grassroot")
        default:
            avatarImg = nil
        }
        
        
        
    }
    
    
    //处理会员等级图片(member:对应的图片)
    private func disposeMembershipImg (member:Int){
            //赋值会员等级
            if member >= 1 && member <= 7 {
                
                membershipImg = UIImage(named: "common_icon_membership_level\(member)")
            }else{
                
                membershipImg = nil
            }
            
        

        
        
        
    }

}
